<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>06_计算器练习</title>
</head>
<body>
<input type="text" placeholder="请输入数字1" id="i1"> <br>
<input type="text" placeholder="请输入数字2" id="i2">
<br>
<button onclick="f1()">加</button>
<button onclick="f2()">减</button>
<button onclick="f3()">乘</button>
<button onclick="f4()">除</button>
<h1>运算结果:<span></span></h1>
<script>
    function f1(){
        let input1 = document.getElementById('i1');
        let input2 = document.getElementById('i2');
        let i1 = parseFloat(input1.value);
        let i2 = parseFloat(input2.value);
        let h1 = document.querySelector("span");
        h1.innerHTML = i1 + i2;
    }
    function f2(){
        let input1 = document.getElementById('i1');
        let input2 = document.getElementById('i2');
        let i1 = parseFloat(input1.value);
        let i2 = parseFloat(input2.value);
        let h1 = document.querySelector("span");
        h1.innerHTML = i1-i2;
    }
    function f3(){
        let input1 = document.getElementById('i1');
        let input2 = document.getElementById('i2');
        let i1 = parseFloat(input1.value);
        let i2 = parseFloat(input2.value);
        let h1 = document.querySelector("span");
        h1.innerHTML = i1*i2;
    }
    function f4(){
        let input1 = document.getElementById('i1');
        let input2 = document.getElementById('i2');
        let i1 = parseFloat(input1.value);
        let i2 = parseFloat(input2.value);
        if (i2!=0){
            let h1 = document.querySelector("span");
            h1.innerHTML = i1/i2;
        }else {
            alert('除数不能为0!');
        }
    }
</script>

</body>
</html>